import { NextRequest } from 'next/server';
import { withMiddleware } from '@/lib/middleware/entry';
import { successResponse, errorResponse } from '@/lib/response';
import { prisma } from '@/lib/prisma';
import { authenticateRequest } from '@/utils/auth';

async function handler(req: NextRequest) {
  const authResult = await authenticateRequest(req);
  if (!authResult.success) {
    return errorResponse(authResult.error || '身份验证失败');
  }

  const { id } = (await req.json()) as { id: string };

  if (!id) {
    return errorResponse('缺少分类ID');
  }

  await prisma.category.delete({
    where: { id },
  });

  return successResponse(null, '删除成功');
}

export const POST = withMiddleware(handler);